package cn.cloud.all.security.oauth2.client.provider;

import cn.cloud.all.security.access.AccessDeniedException;
import cn.cloud.all.security.oauth2.client.exception.OAuth2AccessDeniedException;
import cn.cloud.all.security.oauth2.client.exception.UserRedirectRequiredException;
import cn.cloud.all.security.oauth2.client.token.AccessTokenRequest;
import cn.cloud.all.security.oauth2.common.OAuth2AccessToken;
import cn.cloud.all.security.oauth2.common.OAuth2RefreshToken;
import cn.cloud.all.security.oauth2.resource.ClientCredentialsResourceDetails;
import cn.cloud.all.security.oauth2.resource.OAuth2ProtectedResourceDetails;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Iterator;
import java.util.List;

public class ClientCredentialsAccessTokenProvider extends OAuth2AccessTokenSupport implements AccessTokenProvider {

    public boolean supportsResource(OAuth2ProtectedResourceDetails resource) {
        return resource instanceof ClientCredentialsResourceDetails && "client_credentials".equals(resource.getGrantType());
    }

    public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource) {
        return false;
    }

    public OAuth2AccessToken refreshAccessToken(OAuth2ProtectedResourceDetails resource, OAuth2RefreshToken refreshToken, AccessTokenRequest request) throws UserRedirectRequiredException {
        return null;
    }

    public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails details, AccessTokenRequest request) throws UserRedirectRequiredException, AccessDeniedException, OAuth2AccessDeniedException {

        ClientCredentialsResourceDetails resource = (ClientCredentialsResourceDetails) details;
        return retrieveToken(request, resource, getParametersForTokenRequest(resource), new HttpHeaders());
    }

    private MultiValueMap<String, String> getParametersForTokenRequest(ClientCredentialsResourceDetails resource) {

        MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
        form.set("grant_type", "client_credentials");

        if (resource.isScoped()) {
            StringBuilder builder = new StringBuilder();
            List<String> scope = resource.getScope();

            if (scope != null) {
                Iterator<String> scopeIt = scope.iterator();
                while (scopeIt.hasNext()) {
                    builder.append(scopeIt.next());
                    if (scopeIt.hasNext()) {
                        builder.append(' ');
                    }
                }
            }
            form.set("scope", builder.toString());
        }
        return form;
    }
}
